package net.wangliping.android.tools.microblog;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

public class MicroBlogData {

	private final String LOG_TAG = "MicroBlogData";
	private final String DB_FILE = "microblog.db";
	private final String USER_TABLE = "usrpwd";
	
	public final static String COL_SITE    = "site";
	public final static String COL_USER    = "username";
	public final static String COL_PWD     = "password";
	public final static String COL_EN_PWD  = "pwdencoding";
	public final static String COL_API_KEY = "apikey";
	public final static String COL_ENABLE  = "enable";
	public final static String COL_MORE    = "moreinfo";
	
	public final static String SITE_F5 = "follow5";
	public final static String SITE_QQ = "qq";

	private final String[] usrColumnNames = { COL_SITE, COL_USER, COL_PWD,
			COL_EN_PWD, COL_API_KEY, COL_ENABLE, COL_MORE };

	private DatabaseAdapter db;
	
	public MicroBlogData(Context ctx) {
		db = new DatabaseAdapter(ctx, DB_FILE);
		db.createTable(USER_TABLE, usrColumnNames);
	}
	
	public void updateUsers(ContentValues values) {
		if (db.getValues(USER_TABLE, COL_SITE, values.getAsString(COL_SITE)) != null) {
			db.update(USER_TABLE, values, COL_SITE);
		} else {
			db.insert(USER_TABLE, values);
		}
	}
	
	public void updateUsers(String site, String user, String pwd, String enpwd, String key,
			String enable, String more) {
		ContentValues values = new ContentValues();
		values.put(MicroBlogData.COL_SITE, TextUtils.isEmpty(site) ? "" : site);
		values.put(MicroBlogData.COL_USER, TextUtils.isEmpty(user) ? "" : user);
		values.put(MicroBlogData.COL_PWD, TextUtils.isEmpty(pwd) ? "" : pwd);
		values.put(MicroBlogData.COL_EN_PWD, TextUtils.isEmpty(enpwd) ? "" : enpwd);
		values.put(MicroBlogData.COL_API_KEY, TextUtils.isEmpty(key) ? "" : key);
		values.put(MicroBlogData.COL_ENABLE, TextUtils.isEmpty(enable) ? "" : enable);		
		values.put(MicroBlogData.COL_MORE, TextUtils.isEmpty(more) ? "" : more);
		
		updateUsers(values);
	}
	
	public HashMap<String, String> getUserValues(String site) {
		return db.getValues(USER_TABLE, COL_SITE, site);
	}
	
	public void close() {
		db.close();
	}
	
	private class DatabaseAdapter {
		private SQLiteDatabase mDataBase;
		private int mColumn;
		
		public DatabaseAdapter(Context context, String dbName) {
			open(context, dbName);
		}
		
		public void open(Context context, String dbName) {
			if (null == mDataBase || !this.mDataBase.isOpen()) {
				this.mDataBase = context.openOrCreateDatabase(dbName,
						Context.MODE_PRIVATE, null);
			}
		}
		
		public void close() {
			mDataBase.close();
	    }		

		public void createTable(String tableName, String[] columnNames) {
			StringBuffer sql = new StringBuffer("CREATE TABLE IF NOT EXISTS ");
			sql.append(tableName);
			sql.append(" (");

			mColumn = columnNames.length;
			for (int i = 0; i < mColumn; i++) {
				sql.append(columnNames[i]);
				sql.append(" varchar");
				if (i < mColumn - 1) {
					sql.append(" , ");
				}
			}
			sql.append(" );");
			mDataBase.execSQL(sql.toString());
		}
		
		public void insert(String tableName, ContentValues values) {
			if (mColumn != values.size()) {
				Log.w(LOG_TAG, "INSERT: The size of items is not right");
				return;
			}

			mDataBase.insert(tableName, null, values);
		}
		
		public void update(String tableName, ContentValues values, String key) {
			if (mColumn != values.size()) {
				Log.w(LOG_TAG, "UPDATE: The size of items is not right");
				return;
			}

			mDataBase.update(tableName, values, key + "=?",
					new String[] { values.getAsString(key) });
		}
		
		public HashMap<String, String> getValues(String tablename, String key, String value)
				throws SQLException {
			String sql = "select * from " + tablename + " where " + key + "=?";
			Cursor cursor = mDataBase.rawQuery(sql, new String[] { value });
			if (cursor.getCount() > 0) {
				HashMap<String, String> map = new HashMap<String, String>();
				cursor.moveToFirst();
				int len = cursor.getColumnCount();
				for (int i = 0; i < len; i++) {
					map.put(cursor.getColumnName(i), cursor.getString(i));
				}
				cursor.close();
				return map;
			}
			return null;
		}
	}
}
